from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
import time


# 镜像定制页面-进入流水线
class PiplineBuild(object):
    def __init__(self, driver):
        # 浏览器驱动
        self.driver = driver
        # 页面地址
        self.driver.get("http://123.249.10.3:30109/images")
        self.driver.implicitly_wait(15)

    # 流水线页面信息检查
    def check_pipline_info(self):
        try:
            pip_one = self.driver.find_element(By.CLASS_NAME, 'span-description')
            pip_name = pip_one.text
            pip_one.click()
        except Exception as e:
            message = '没有流水线'
            print(e)
            print(message)
        time.sleep(3)

        # 标签检查
        label_overview = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[1]/div[1]')
        label_history = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[1]/div[2]')
        label_user = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[1]/div[3]')

        # 标题检查
        pipline_name = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/aside/div[1]/p')
        if pip_name == pipline_name.text:
            name_value = 1
        else:
            name_value = 0

        # 图标检查
        icon_done = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                       '2]/section/section/aside/div[2]/span[1]')
        icon_doing = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                        '2]/section/section/aside/div[2]/span[2]')
        icon_todo = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                       '2]/section/section/aside/div[2]/span[3]')

        return [label_overview, label_history, label_user, name_value, icon_done, icon_doing, icon_todo]

    def check_start_info(self):
        self.driver.implicitly_wait(15)
        try:
            pip_one = self.driver.find_element(By.CLASS_NAME, 'span-description')
            pip_name = pip_one.text
            pip_one.click()
        except Exception as e:
            message = '没有流水线'
            print(e)
            print(message)

        start_button = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/aside/div[3]/div[1]/div[1]')
        start_info = self.driver.find_element(By.XPATH,
                                              '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                              '2]/section/section/main/div/p')
        pipline_type = self.driver.find_element(By.XPATH,
                                                '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                '2]/section/section/main/div/table/tr[1]/td[1]')
        pipline_group = self.driver.find_element(By.XPATH,
                                                 '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                 '2]/section/section/main/div/table/tr[2]/td[1]')
        pipline_name = self.driver.find_element(By.XPATH,
                                                '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                '2]/section/section/main/div/table/tr[3]/td[1]')
        image_format = self.driver.find_element(By.XPATH,
                                                '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                '2]/section/section/main/div/table/tr[4]/td[1]')
        arch = self.driver.find_element(By.XPATH,
                                        '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                        '2]/section/section/main/div/table/tr[5]/td[1]')
        return [start_button, start_info, pipline_type, pipline_group, pipline_name, image_format, arch]